package com.gettec.lims.client.bind;

import com.extjs.gxt.ui.client.binding.Converter;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.Field;


public class ComboBoxFieldBinding extends FieldBinding {

    public ComboBoxFieldBinding(ComboBox field, String property) {
        super(field, property);
        if (field instanceof ComboBox) {
            final ComboBox<ModelData> combo = (ComboBox<ModelData>) field;
            setConverter(new Converter() {

                @Override
                public Object convertFieldValue(Object value) {
                    if (value instanceof ModelData) {
                        ModelData val = (ModelData) value;
                        Object v=val.get(combo.getValueField());
                        return v;
                    } else {
                        return value;
                    }
                }

                @Override
                public Object convertModelValue(Object value) {
                    return combo.getStore().findModel(combo.getValueField(), value);
                }

            });
        }
    }
    public ComboBoxFieldBinding(ComboBox field)
    {
    	this(field,field.getName());
    }

}